\input{tex/question05.tex}

\subsection{标准答案}

\textbf{使用 std::function 并擦除类型}

\begin{minted}[mathescape,	
    linenos,
    numbersep=5pt,
    gobble=2,
    frame=lines,
    framesep=2mm,
    breaklines]{c++}
    struct scope_guard {
        std::function<void()>f;
        template<typename Func, typename...Args> requires std::invocable<Func, std::unwrap_reference_t<Args>...>
        scope_guard(Func&& func, Args&&...args) :f{ [func = std::forward<Func>(func), ...args = std::forward<Args>(args)]() mutable {
                std::invoke(std::forward<std::decay_t<Func>>(func), std::unwrap_reference_t<Args>(std::forward<Args>(args))...);
            } }{}
        ~scope_guard() { f(); }
        scope_guard(const scope_guard&) = delete;
        scope_guard& operator=(const scope_guard&) = delete;
    };
\end{minted}

\textbf{使用 std::tuple+std::apply}

\begin{minted}[mathescape,	
    linenos,
    numbersep=5pt,
    gobble=2,
    frame=lines,
    framesep=2mm,
    breaklines]{c++}
    template<typename F, typename...Args>
        requires requires(F f, Args...args) { std::invoke(f, args...); }
    struct scope_guard {
        F f;
        std::tuple<Args...>values;

        template<typename Fn, typename...Ts>
        scope_guard(Fn&& func, Ts&&...args) :f{ std::forward<Fn>(func) }, values{ std::forward<Ts>(args)... } {}
        ~scope_guard() {
            std::apply(f, values);
        }
        scope_guard(const scope_guard&) = delete;
    };

    template<typename F, typename...Args>//推导指引非常重要
    scope_guard(F&&, Args&&...) -> scope_guard<std::decay_t<F>, std::decay_t<Args>...>;
\end{minted}

\subsection{解析}

\clearpage